package com.hoshiicloud.saleorder.controller.sendorder;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.saleorder.entity.DispatchBill;
import com.hoshiicloud.saleorder.feign.IdGeneratorService;
import com.hoshiicloud.saleorder.param.DispatchBillAddParam;
import com.hoshiicloud.saleorder.param.DispatchBillQueryParam;
import com.hoshiicloud.saleorder.param.DispatchBillUpdateParam;
import com.hoshiicloud.saleorder.service.sendOrder.DispatchBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:campchen
 * @Date:Created in 下午 07:19 2019/8/22
 * @PACKAGENAME com.hoshiicloud.saleorder.controller.sendOrder
 * @Description
 */
@Slf4j
@RestController
@RequestMapping(value = "/orderSend/dispatchBill")
@Api(value = "出库单", tags = {"出库单"})
public class DispatchBillController {

    @Autowired
    private DispatchBillService dispatchBillDetailService;

    @Autowired
    private IdGeneratorService idGeneratorService;


    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询发货单(O2O)
     */
    @ApiOperation(value = "分页查询发货单(O2O)")
    @PostMapping("/getPage")
    public Response page(@RequestBody DispatchBillQueryParam param) {
        try {
            QueryWrapper<DispatchBill> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(param.getCompanyId() != null, DispatchBill::getCompanyId, param.getCompanyId())
                    .eq(param.getPlatformId() != null, DispatchBill::getPlatformId, param.getPlatformId())
                    .eq(param.getStoreId() != null, DispatchBill::getStoreId, param.getStoreId())
                    .eq(StringUtils.isNotBlank(param.getBillNo()), DispatchBill::getBillNo, param.getBillNo())
                    .eq(StringUtils.isNotBlank(param.getStoreName()), DispatchBill::getStoreName, param.getStoreName())
                    .eq(StringUtils.isNotBlank(param.getConsignee()), DispatchBill::getConsignee, param.getConsignee())
                    .eq(StringUtils.isNotBlank(param.getConsigneeTelephone()), DispatchBill::getConsigneeTelephone, param.getConsigneeTelephone())
                    .eq(StringUtils.isNotBlank(param.getConsigneeAddress()), DispatchBill::getConsigneeAddress, param.getConsigneeAddress())
                    .eq(StringUtils.isNotBlank(param.getDeliverer()), DispatchBill::getDeliverer, param.getDeliverer())
                    .eq((param.getDeliveryTime() != null), DispatchBill::getDeliveryTime, param.getDeliveryTime())
                    .eq(StringUtils.isNotBlank(param.getActualDeliverer()), DispatchBill::getActualDeliverer, param.getActualDeliverer())
                    .eq((param.getQuantity() != null && param.getQuantity() != 0), DispatchBill::getQuantity, param.getQuantity())
                    .eq((param.getDeleted() != null && param.getDeleted() != 0), DispatchBill::getDeleted, param.getDeleted())
                    .eq(StringUtils.isNotBlank(param.getRemark()), DispatchBill::getRemark, param.getRemark())
                    .eq(param.getStatus() != null, DispatchBill::getStatus, param.getStatus());
            Page<DispatchBill> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(dispatchBillDetailService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询发货单(O2O)异常", e);
            return Response.failed("分页查询发货单(O2O)异常");
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询发货单(O2O)
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询发货单(O2O)")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(dispatchBillDetailService.getById(id));
    }


    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增发货单(O2O)")
    public Response save(@RequestBody @Valid DispatchBillAddParam param) {
        DispatchBill data;
        try {
            data = BeanUtils.covertType(param, DispatchBill.class);
        } catch (Exception e) {
            log.error("新增机构发货单(O2O)", e);
            return Response.failed("新增发货单(O2O) Bean转换异常");
        }
        return Response.data(dispatchBillDetailService.save(data));
    }


    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改发货单(O2O)
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改发货单(O2O)")
    public Response update(@RequestBody @Valid DispatchBillUpdateParam param) {
        DispatchBill data;
        try {
            data = BeanUtils.covertType(param, DispatchBill.class);
        } catch (Exception e) {
            log.error("修改发货单(O2O) Bean转换异常", e);
            return Response.failed("修改发货单(O2O) Bean转换异常");
        }
        return Response.data(dispatchBillDetailService.updateById(data));
    }
}
